(ns fx-clj.controls.table
  (:require [fx-clj.core :as fx]
            [freactive.core :as f]
            [clojure.core.async :refer [chan <! go-loop]])
  (:import (javafx.application Platform)))

(def n (atom 3))

(def state (f/atom {:data [{:id 1 :text "a"}
                           {:id 2 :text "b"}
                           {:id 3 :text "c"}]}))

(let [table-data (f/cursor state :data)]
  (defn create-view []
    (let [ch (chan)
          table (fx/table-view
                  {:columns [(fx/table-column {:text               :id
                                               :cell-value-factory :id})
                             (fx/table-column {:text               "名称"
                                               :cell-value-factory :text})]
                   :items   table-data})
          button (fx/button {:on-action ch} "Add")
          view (fx/v-box table button)]
      (go-loop []
        (<! ch)
        (let [a (swap! n inc)
              c (str (char (+ (int \a) (dec a))))]
          (swap! table-data conj {:id a :text c}))
        (recur))
      view)))

(Platform/setImplicitExit true)
(fx/sandbox #'create-view)